<template>
    <div>
        <el-button type="primary" @click="showCreateDialog">创建角色</el-button>
        
        <el-table :data="roles" border>
            <el-table-column label="角色名称" prop="name"></el-table-column>
            <el-table-column label="角色描述" prop="description"></el-table-column>
            <el-table-column label="操作">
                <template #default="{ row }">
                    <el-button type="primary" @click="editRole(row)">编辑</el-button>
                    <el-button type="danger" @click="deleteRole(row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog title="编辑角色" v-model="dialogVisible" width="30%">
            <el-form :model="currentRole" label-width="100px">
                <el-form-item label="角色名称">
                    <el-input v-model="currentRole.name"></el-input>
                </el-form-item>
                <el-form-item label="角色描述">
                    <el-input v-model="currentRole.description"></el-input>
                </el-form-item>
            </el-form>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="saveRole">保存</el-button>
            </div>
        </el-dialog>


    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref } from 'vue'

const roles = reactive<Role[]>([
    { name: '管理员', description: '拥有所有权限' },
    { name: '普通用户', description: '只能查看和编辑自己的数据' },
])

const dialogVisible = ref(false)

const currentRole = reactive<Role>({
    name: '',
    description: '',
});

function showCreateDialog() {
    currentRole.name = '';
    currentRole.description = '';
    dialogVisible.value = true;
}

function editRole(role: Role) {
    currentRole.name = role.name;
    currentRole.description = role.description;
    dialogVisible.value = true;
}

function saveRole() {
    if (currentRole.name && currentRole.description) {
        // 保存角色到服务器
        console.log('保存角色', currentRole);
        dialogVisible.value = false;
    } else {
        // 提示信息
        console.error('请填写完整信息');
    }
}

function deleteRole(role: Role) {
    // 删除角色
    console.log('删除角色', role);
}
</script>
  
<style lang="less" scoped>
.dialog-footer {
    text-align: right;
}
</style>